package jpf.nextsearch.util.validation;

import java.util.Map;

import jpf.nextsearch.base.GenericObject;
import jpf.nextsearch.util.validation.base.ObjectValidator;
import jpf.nextsearch.util.validation.base.ValidatorManager;
import jpf.nextsearch.util.validation.base.ValidatorNotFoundException;

/**
 * @author <a href="mailto:jacek.pruszkowski@gmail.com">Jacek Pruszkowski</a>
 */
public class ValidatorManagerImpl extends GenericObject implements
		ValidatorManager {
	
	private Map<String, ObjectValidator> validators;
	
	public boolean isValueValid(Object value, String type) {
		if (type == null || "".equals(type.trim())) {
			if (isLoggingError()) {
				getLogger().error("Validation type is NULL or empty");
				throw new ValidatorNotFoundException("Validation type is NULL or empty");
			}
		}
		if (getValidators() == null || getValidators().size() == 0) {
			if (isLoggingError()) {
				getLogger().error("Validators map is NULL or empty");
				throw new ValidatorNotFoundException("Validators map is NULL or empty");
			}
		}
		
		ObjectValidator validator = getValidators().get(type);
		
		if (validator == null) {
			if (isLoggingError()) {
				getLogger().error("Validator was NOT FOUND for type [" + type + "]");
				throw new ValidatorNotFoundException("Validator was NOT FOUND for type [" + type + "]");
			}
			return false;
		}
		
		return validator.isValueValid(value);
		
	}

	public void setValidators(Map<String, ObjectValidator> validators) {
		this.validators = validators;
	}

	public Map<String, ObjectValidator> getValidators() {
		return validators;
	}
	

}
